package com.xinchengxd.user.controller;


import ch.qos.logback.classic.spi.EventArgUtil;
import com.xinchengxd.commons.ResponseResult;
import com.xinchengxd.user.entity.User;
import com.xinchengxd.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author: 姚新成
 * @Date: 2021/11/8:15:20
 * @Description: 用户的注册，登录，token的生成，校验，查看个人信息，修改个人信息，修改密码，修改积分
 */

//@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Value("${server.port}")
    private int port;

    @Resource
    UserService userService;


//    用户注册
    @RequestMapping("/add")
    public ResponseResult<User> Add(@RequestBody User user){
        int reid = userService.add(user);
        log.info("端口是：{}",port);
        return new ResponseResult<User>(200,"ok",null);
    }

//    用户登录
    @RequestMapping("/sign")
    public ResponseResult<String> signIn(@RequestBody User user){

        return userService.sign(user);
    }

//    token验证
    @RequestMapping("/token")
    public ResponseResult<String> verifyToken(String token){
        return userService.verifyToken(token);
    }

//    查看个人信息  /使用请求头解析用户id --21/11/23
    @RequestMapping("/look")
    public ResponseResult<User> look(@RequestHeader("userID") String id){
        Integer userId = Integer.valueOf(id);
        User u = userService.look(userId);
        return new ResponseResult<>(200,"ok",u);
    }

    //    查看个人信息
    @RequestMapping("/lookbyid")
    public ResponseResult<User> lookById(int userId){
        User u = userService.look(userId);
        return new ResponseResult<>(200,"ok",u);
    }

//    修改个人信息
    @RequestMapping("/update")
    public ResponseResult<String> update(@RequestBody User user){
        userService.update(user);
        return new ResponseResult<>(200,"ok","修改完成");
    }

//    修改密码
    @RequestMapping("/uppwd")
    public ResponseResult<String> updateForPassword(String oldPwd,String newPwd,Integer id){
        return userService.updateForPassword(oldPwd,newPwd,id);
    }

//更新积分
    @RequestMapping("/point")
    public ResponseResult<String> changePoint(Integer id , Integer change){
        System.out.println("id"+id+"^"+"change"+change);
       int total = userService.changePoint(id,change);

       return total >0 ? new ResponseResult<>(200,"ok","更新成功")
               : new ResponseResult<>(601,"no","更新失败");
    }
}
